/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class TarjetaDAOImpl implements orm.dao.TarjetaDAO {
	public Tarjeta loadTarjetaByORMID(int idtarjeta) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return loadTarjetaByORMID(session, idtarjeta);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta getTarjetaByORMID(int idtarjeta) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return getTarjetaByORMID(session, idtarjeta);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByORMID(int idtarjeta, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return loadTarjetaByORMID(session, idtarjeta, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta getTarjetaByORMID(int idtarjeta, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return getTarjetaByORMID(session, idtarjeta, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByORMID(PersistentSession session, int idtarjeta) throws PersistentException {
		try {
			return (Tarjeta) session.load(orm.Tarjeta.class, new Integer(idtarjeta));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta getTarjetaByORMID(PersistentSession session, int idtarjeta) throws PersistentException {
		try {
			return (Tarjeta) session.get(orm.Tarjeta.class, new Integer(idtarjeta));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByORMID(PersistentSession session, int idtarjeta, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Tarjeta) session.load(orm.Tarjeta.class, new Integer(idtarjeta), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta getTarjetaByORMID(PersistentSession session, int idtarjeta, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Tarjeta) session.get(orm.Tarjeta.class, new Integer(idtarjeta), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta[] listTarjetaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return listTarjetaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta[] listTarjetaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return listTarjetaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta[] listTarjetaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tarjeta as Tarjeta");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Tarjeta[]) list.toArray(new Tarjeta[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta[] listTarjetaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tarjeta as Tarjeta");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Tarjeta[]) list.toArray(new Tarjeta[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return loadTarjetaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return loadTarjetaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Tarjeta[] tarjetas = listTarjetaByQuery(session, condition, orderBy);
		if (tarjetas != null && tarjetas.length > 0)
			return tarjetas[0];
		else
			return null;
	}
	
	public Tarjeta loadTarjetaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Tarjeta[] tarjetas = listTarjetaByQuery(session, condition, orderBy, lockMode);
		if (tarjetas != null && tarjetas.length > 0)
			return tarjetas[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateTarjetaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return iterateTarjetaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTarjetaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PinpassPersistentManager.instance().getSession();
			return iterateTarjetaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTarjetaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tarjeta as Tarjeta");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTarjetaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tarjeta as Tarjeta");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta createTarjeta() {
		return new orm.Tarjeta();
	}
	
	public boolean save(orm.Tarjeta tarjeta) throws PersistentException {
		try {
			orm.PinpassPersistentManager.instance().saveObject(tarjeta);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Tarjeta tarjeta) throws PersistentException {
		try {
			orm.PinpassPersistentManager.instance().deleteObject(tarjeta);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Tarjeta tarjeta)throws PersistentException {
		try {
			if(tarjeta.getCliente_idcliente() != null) {
				tarjeta.getCliente_idcliente().setTarjeta(null);
			}
			
			orm.Coordenada[] lCoordenadas = tarjeta.coordenada.toArray();
			for(int i = 0; i < lCoordenadas.length; i++) {
				lCoordenadas[i].setTarjeta_idtarjeta(null);
			}
			return delete(tarjeta);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Tarjeta tarjeta, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(tarjeta.getCliente_idcliente() != null) {
				tarjeta.getCliente_idcliente().setTarjeta(null);
			}
			
			orm.Coordenada[] lCoordenadas = tarjeta.coordenada.toArray();
			for(int i = 0; i < lCoordenadas.length; i++) {
				lCoordenadas[i].setTarjeta_idtarjeta(null);
			}
			try {
				session.delete(tarjeta);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Tarjeta tarjeta) throws PersistentException {
		try {
			orm.PinpassPersistentManager.instance().getSession().refresh(tarjeta);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Tarjeta tarjeta) throws PersistentException {
		try {
			orm.PinpassPersistentManager.instance().getSession().evict(tarjeta);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tarjeta loadTarjetaByCriteria(TarjetaCriteria tarjetaCriteria) {
		Tarjeta[] tarjetas = listTarjetaByCriteria(tarjetaCriteria);
		if(tarjetas == null || tarjetas.length == 0) {
			return null;
		}
		return tarjetas[0];
	}
	
	public Tarjeta[] listTarjetaByCriteria(TarjetaCriteria tarjetaCriteria) {
		return tarjetaCriteria.listTarjeta();
	}
}
